package com.axones.planningpoker.rest.service;

import org.restlet.data.Status;
import org.restlet.resource.Delete;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

import com.axones.planningpoker.bean.Item;
import com.axones.planningpoker.dao.ItemDAO;
import com.google.gson.Gson;

public class ItemResource extends ServerResource {
	private static ItemDAO iDao;
	private static Gson gson;

	public ItemResource() {
		if (iDao == null) {
			iDao = new ItemDAO();
		}
		if (gson == null) {
			gson = new Gson();
		}
	}

	@Post
	public String doCreate(String i) {
		Item item = gson.fromJson(i, Item.class);

		Long l = iDao.add(item);

		setStatus(Status.SUCCESS_CREATED);

		return l.toString();
	}

	@Put
	public void doUpdate(String i) {
		Item item = gson.fromJson(i, Item.class);

		iDao.update(item);

		setStatus(Status.SUCCESS_OK);
	}

	@Delete
	public void doDelete() {
		Long id = Long.parseLong((String) getRequest().getAttributes()
				.get("id"));

		iDao.delete(id);

		setStatus(Status.SUCCESS_OK);
	}

}
